from fastapi import FastAPI
from starlette.responses import HTMLResponse
import uvicorn
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
import os
from pyecharts.components import Table
from pyecharts.options import ComponentTitleOpts

##定义左右格式

html1='''
<html>
<head>
<meta http-equiv="refresh" content="10">
</head>
<body>
<div id="header" style="clear:both;text-align:center;">
<h2 style="margin-bottom:0;">左右分屏展示</h2></div>
<div id="container" style="width:1200px"></div>
<div id="left11" style="width:600px;heigth:600px;float:left;">{}</div>
<div id="right2" style="width:600px;heigth:600px;float:left;">{}</div>

<div id="footer" style="clear:both;text-align:center;">
XX中国IT团队</div>
</div>
</body>
</html>
'''

def pie():
    print([list(z) for z in zip(Faker.choose(), Faker.values())])
    c=Pie(opts.InitOpts(width="500px",height="500px",animation_opts=opts.AnimationOpts(animation=False)),)
    c.add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
    c.set_global_opts(

        legend_opts=opts.LegendOpts(is_show=False),

       

    )
    return c.render_embed()

def table():
    table = Table()

    headers = ["City name", "Area", "Population", "Annual Rainfall"]
    rows = [
        ["Brisbane", 5905, 1857594, 1146.4],
        ["Adelaide", 1295, 1158259, 600.5],
        ["Darwin", 112, 120900, 1714.7],
        ["Hobart", 1357, 205556, 619.5],
        ["Sydney", 2058, 4336374, 1214.8],
        ["Melbourne", 1566, 3806092, 646.9],
        ["Perth", 5386, 1554769, 869.4],
    ]
    table.add(headers, rows)
    table.set_global_opts(
        title_opts=ComponentTitleOpts(title="Table-基本示例", subtitle="我是副标题支持换行哦")
    )
    return table.render_embed()

app = FastAPI()




@app.get("/")
def home():

    return HTMLResponse(content=html1.format(pie(),table()
                                             ))



if __name__ == "__main__":

    print('当前程序名称为:',os.path.basename(__file__))
    filename=os.path.basename(__file__).split('.')[0]
    
    
    
    

    uvicorn.run(app, host="0.0.0.0",port=8000,log_level="info")
